/*
 *  Copyright 2007 pollux.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.poxd.frisar;

import org.poxd.frisar.ui.ComponentRendererPane;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;
import javax.swing.plaf.ComponentUI;
import org.poxd.frisar.model.DefaultFriseModel;
import org.poxd.frisar.ui.FriseRenderer;
import org.poxd.frisar.model.FriseModel;
import javax.swing.JComponent;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import org.poxd.frisar.ui.DefaultFriseRenderer;
import org.poxd.frisar.lnf.FrisarUI;
import org.poxd.frisar.model.ElementId;

/**
 *TODO: implement Scrollable
 * TODO: listen to model events and dispatch to the context
 * @author pollux
 */
public class Frisar extends JComponent implements Scrollable {

    private FriseModel model;
    protected int friseHeight;
    protected int friseMargin;
    protected int sizeMode;
    protected Map<String, FriseRenderer> renderers;
    protected ComponentRendererPane componentPane;
    //TODO: échelle des éléments : définir unité !tenir compte des trous!
    private double unit;
    private ScaleContext unitContext;
    private static final String uiClassID = "FrisarUI";
    private static final int AUTO_SIZE_ELEMENTS = 0;
    private static final int USER_DEFINED_SIZE_ELEMENTS = 1;
    private final int MAX_HEIGHT = 400;

    static
    {
        UIManager.getDefaults().put("FrisarUI",
                "org.poxd.frisar.lnf.BasicFrisarUI");
    }

    public Frisar()
    {
        this(null);
    }

    public Frisar(FriseModel model)
    {
        //TODO: create a selection listener
        if (model == null)
        {
            model = createDefaultModel();
        }
        setModel(model);
        initializeLocalVars();
    }
    
    protected void initializeLocalVars()
    {
        friseHeight = 75;
        friseMargin = 10;
        this.sizeMode = AUTO_SIZE_ELEMENTS;
        renderers = new HashMap<String, FriseRenderer>();
//        unitContext = new ScaleContext(this);
//        unitContext.setStrategy(new UserScaleStrategy());

        createDefaultRenderers();
        updateUI();
    }

    protected void createDefaultRenderers()
    {
        FriseRenderer def = new DefaultFriseRenderer(this);
        renderers.put(def.getName(), def);
    }
    
    private FriseModel createDefaultModel()
    {
        return new DefaultFriseModel();
    }

    public ScaleContext getUnitContext()
    {
        return unitContext;
    }
    
    public double getUnit()
    {
        return unit;
    }

    public FriseModel getModel()
    {
        return model;
    }

    public void setModel(FriseModel model)
    {
        if (model == null)
        {
            throw new IllegalArgumentException("Cannot set a null FriseModel");
        }
        FriseModel old = this.model;
        if (old != model)
        {
            if (old != null)
            {
            //@TODO: supprimer les écouteurs sur old
            }
            this.model = model;
            //@TODO: ajouter un écouteur sur le model
            firePropertyChange("model", old, model);
            repaint();
        }
    }

    

    protected void setSizeMode(int sizeMode)
    {
        if ((sizeMode == AUTO_SIZE_ELEMENTS || sizeMode == USER_DEFINED_SIZE_ELEMENTS) && sizeMode != this.sizeMode)
        {
            int oldSizeMode = this.sizeMode;
            this.sizeMode = sizeMode;
            firePropertyChange("sizeMode", oldSizeMode, sizeMode);
            repaint();
        }
    }

    public void setHeight(int height)
    {
        this.friseHeight = height;
    }

    @Override
    public int getHeight()
    {
        return friseHeight;
    }

    //TODO : getElementRect
    public Rectangle getElementRect(ElementId id)
    {
        return null;
    }

    public ComponentUI getUi()
    {
        return ui;
    }

    protected void setUI(FrisarUI newUI)
    {
        super.setUI(newUI);
    }

    @Override
    public void updateUI()
    {
        setUI((FrisarUI) UIManager.getUI(this));
    }

    @Override
    public String getUIClassID()
    {
        return uiClassID;
    }

    public Dimension getPreferredScrollableViewportSize()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getScrollableUnitIncrement(Rectangle arg0, int arg1, int arg2)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getScrollableBlockIncrement(Rectangle arg0, int arg1, int arg2)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean getScrollableTracksViewportWidth()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean getScrollableTracksViewportHeight()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
